package TUILayer;//makes this class a part of the TUILayer package
import java.util.*;
import ControlLayer.DVDCtr;//The TUILayer needs to know the dvd controller in the ControlLayer
import ModelLayer.DVD; //The TUILayer needs to know class from the Modellayer to create an instance of DVD

/**
 * User interface to the DVD controller
 * @author(kbh)
 * @version(2005.03.31)
 * (FEN): 2006.02.24: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 * (KBH): 2008.10.24: dvdmenu changed to If                   
 * (GROUP 4): 2012.11.14: made everything a lot more awesome.
 */

public class DvdUI
{
    private DVDCtr dvdCtr;//DVD UI must know the DVD controller 
    
   /**
     * Constructor for objects of class DVDCtr
     */
    public DvdUI()
    {
        dvdCtr =new DVDCtr();
    }
    
     /**
     * initiating method for the dvd menu.
     */
    public void start()
    {
        dvdMenu();
    }
    
   
    /**
     *  Returns the menu, from where the user can make a choise.
     */
    public void dvdMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choise =   writeDVDMenu();
            if (choise == 1){
                findDVD();
            }
            if(choise == 2){
                createDVD();
            }
            if(choise == 3){
                createCopy();
            }
            if(choise == 4){
                deleteDVD();
            }
            if(choise == 5){
                listAllDvds();
            }                
            if(choise ==6){
                exit = true;
                System.out.print("\f");
            }
            else if(choise > 6){
                System.out.println("\fInvalid choise, please try again");
            }
        }//end else
    }//end else
    
    /**
     * prints range of choices and takes an input from user.
     */
    private int writeDVDMenu()
    {
           // creates an object keyboard to read data from the keyboard
            int choise = 0;
            Scanner keyboard = new Scanner(System.in);
            System.out.println("*** DVD Menu ***");
            System.out.println(" (1) Show DVD information");
            System.out.println(" (2) Create DVD description");
            System.out.println(" (3) Create DVD specimen");
            System.out.println(" (4) Delete DVD");
            System.out.println(" (5) Show current DVD list");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choise: ");
            
           
            try
            {
                choise = keyboard.nextInt();
            }
            catch(Exception e)
            {   
                dvdMenu();
                printNumError();     
            }
            return choise;
    }
    
    /**
     * takes a scanner input of id and verifies it as type long before passing it on.
     */
    public long inputDvdId()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\fType DVD number: ");
        long id = 0;
        try {
            id = keyboard.nextLong();
        }
        catch(InputMismatchException e)
        {
            printNumError();
        }
        return id;
    }
    
     /**
     * Methods for capturing information to pass on 
     */
    private String inputTitle()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD title: " );
        String title = keyboard.nextLine();
        return title;
    }
  
    private int inputCopyNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the copy number: ");
        int no = keyboard.nextInt();
        return no;
    }
   
    private String inputArtist()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD artist: " );
        String artist = keyboard.nextLine();
        return artist;
    }
    
    private String inputReleaseDate()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD release date: " );
        String releaseDate = keyboard.nextLine();
        return releaseDate;
    }
    
    private int inputSerialNr()
    {
        // creates an object keyboard to read data from the keyboard
        int no = 0;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the serial number: ");
        try
        {
            no = keyboard.nextInt();
        }
        catch(Exception e)
        {
            printNumError();
        }
        return no;
    }
    
    private String inputPurchaseDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type purchase date: " );
        String purchaseDate = keyboard.nextLine();
        return purchaseDate;
    }
    
    private int inputPurchasePrice()
    {
        // creates an object keyboard to read data from the keyboard
        int purchasePrice = 0;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD price: " );
        try
        {
            purchasePrice = keyboard.nextInt();
        }
        catch(Exception e)
        {
            printNumError();
        }
        return purchasePrice;
    }
    
    /**
     * Returns an object of type DVD
     */
    public DVD getDVD(long id)
    {//is to be changed when the address book controller is completed
        return dvdCtr.getDVD(id);
    }
    
    /**
     * takes a scanner input of id and verifies it as type long before passing it on.
     */
    public long getId()
    {    
        long i = 0;
        Scanner keyboard = new Scanner(System.in);  
        System.out.println("Write DVD id:  ");
        try
        {
            i = keyboard.nextLong();
        }
        catch(Exception e)
        {
            printNumError();
        }

        return i;
    }
    
    /**
     * takes an input of id and returns true if a match exists.
     */
     public boolean checkForValid(long i)
    {
        boolean valid = false;
        if (dvdCtr.getDVD(i) != null)
        {
            valid = true;  
        }
        return valid;
    }
    
    /**
     * takes a valid scanner input of id, checks for a match in container class 
     * and prints informations of the dvd instance if found
     */
    private void findDVD()
    {
        System.out.println("\f You have chosen to show info about a DVD");  
        long i = getId();
        boolean valid = checkForValid(i);
        
         while (valid == false)
         {
                System.out.println("\fId not found, try again. Type 0 (zero) to exit.");
                i = getId();
                valid = checkForValid(i);
                
                if(i == 0)
                
                {
                    valid = true;
                    System.out.print("\f");
                }

            }
        
        if (checkForValid(i) == true)
            {  
            System.out.println("\f*****************************");
            System.out.println("ID: " + getDVD(i).getId() );
            System.out.println("Title: " + getDVD(i).getTitle() );
            System.out.println("Artist: " + getDVD(i).getArtist() );
            System.out.println("Release date: " + getDVD(i).getReleaseDate());
            System.out.println("Copies: " + getDVD(i).getCopies());
            System.out.println("*****************************");
            System.out.println("\n");
        }
       
    }
    
    
    /**
     * creates a new instance of DVD and adds it to a containerclass
     */
    public void createDVD()
    {
        System.out.println("\fYou have chosen to add a new DVD");
            
            DVDCtr dvdCtr = new DVDCtr();
            String title = inputTitle();
            String artist = inputArtist();
            String releaseDate = inputReleaseDate();
            dvdCtr.createDVD(title, artist, releaseDate);
            
       System.out.println("\fThe DVD has been created.\n");
       
    }
    
    /**
     * validates that an input of id has a match in a containerclass 
     */
    public boolean valid(long i)
    {
        boolean valid = false;
        if (dvdCtr.getDVD(i) != null)
        {
            valid = true;  
        }
            
             
         while (valid == false)
         {
                System.out.println("\fId not found, try again. Type 0 (zero) to exit.");
                i = getId();
                valid = checkForValid(i);
                
                
                if(i == 0)
                
                {
                    valid = true;
                    System.out.print("\f");
                }

            }
         return valid;   
    }
    
    /**
     * creates a new copy and adds it to an instance of DVD based on a validated existing input of id 
     */
    private void createCopy()
    {
        System.out.println("\fYou have chosen to add a new copy of a DVD");
        long i = getId();
        boolean valid = checkForValid(i);
        
         while (valid == false)
         {
                System.out.println("\fId not found, try again. Type 0 (zero) to exit.");
                i = getId();
                valid = checkForValid(i);
                if(i == 0)
                {
                    valid = true;
                    System.out.print("\f");
                }
         }
         
         if (checkForValid(i) == true)
            {
                String newPurchaseDate = inputPurchaseDate();
                int newPurchasePrice = inputPurchasePrice();
                dvdCtr.createCopy(i, newPurchaseDate, newPurchasePrice);
                System.out.print("\fA copy of the dvd has been created.\n");
            }      
    }
    
     /**
     *  takes an input of type id, checks it for validity and removes a dvd if found.
     */
    private void deleteDVD()
    {
        System.out.println("\f You have chosen to delete a DVD");  
        long i = getId(); 
        boolean valid = checkForValid(i);
         while (valid == false)
         {
                System.out.println("\fId not found, try again. Type 0 (zero) to exit.");
                i = getId();
                valid = checkForValid(i);
                if(i == 0)
                {
                    valid = true;
                }
            }
        
        if (checkForValid(i) == true)
            {
                dvdCtr.deleteDVD(i);
                System.out.println("\fThe DVD has been deleted.\n");
            }
    }
    
    /**
    * prints a list of all dvds registered in the system
    */
    public void listAllDvds()
    {
        System.out.println("\fList of all DVD's: ");
        System.out.println("----------------------");
        dvdCtr.listAllDvds();
        System.out.println("----------------------");
        System.out.print("\n");
    }
    
    /**
     * returns an error message in case of invalidt input
     */ 
    private void printNumError()
    {
        System.out.println("\f Invalid input, it has to be a number. \n");
    }
    

    
}